
 <!DOCTYPE HTML>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  
    <title>Kubernetes 1.8 基础集群搭建 | Zong&#39;s blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="Zong">
    
    <meta name="description" content="Kubernetes简称K8s，是Google推出的容器集群，个人感觉对比docker原生的swarm，K8s更全面当然也更复杂。具体的对Kubernetes的介绍可以网上找，这里不多说了，下面开始记录一下手工搭建一个简单集群的步骤。首先你的主机linux内核需要3.1以上，推荐ubuntu 16.">
    
    
    
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
</head>
</html>
  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.svg" alt="Zong&#39;s blog" title="Zong&#39;s blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Zong&#39;s blog">Zong&#39;s blog</a></h1>
				<h2 class="blog-motto">日常积累，技术分享</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">Home</a></li>
					
						<li><a href="/archives">Archives</a></li>
					
						<li><a href="/categories/运维">运维</a></li>
					
						<li><a href="/categories/容器架构">容器架构</a></li>
					
					<li>
					
					<form class="search" action="//baidu.com/s" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="text" id="search" name="wd" autocomplete="off" maxlength="20" placeholder="Search" />
                        <input name=tn type=hidden value="bds">
                        <input name=cl type=hidden value="3">
                        <input name=ct type=hidden value="2097152">
						<input type="hidden" name="si" value="www.lstop.pub">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/11/29/Kubernetes-1-8-基础集群搭建/" title="Kubernetes 1.8 基础集群搭建" itemprop="url">Kubernetes 1.8 基础集群搭建</a>
  </h1>
  <p class="article-author">By
    
      <a href="http://www.lstop.pub" title="Zong">Zong</a>
    </p>
  <p class="article-time">
    <time datetime="2017-11-29T02:15:40.000Z" itemprop="datePublished">2017-11-29</time>
    
  </p>
</header>

	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、etcd"><span class="toc-number">1.</span> <span class="toc-text">一、etcd</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、SSL证书"><span class="toc-number">2.</span> <span class="toc-text">二、SSL证书</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、master端"><span class="toc-number">3.</span> <span class="toc-text">三、master端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-kube-apiserver服务"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 kube-apiserver服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-kube-controller-manager服务"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 kube-controller-manager服务</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、node端"><span class="toc-number">4.</span> <span class="toc-text">四、node端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-kubele"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 kubele</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-kube-proxy"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 kube-proxy</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#五、测试"><span class="toc-number">5.</span> <span class="toc-text">五、测试</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#六、后续"><span class="toc-number">6.</span> <span class="toc-text">六、后续</span></a></li></ol>
		</div>
		
		<p>Kubernetes简称K8s，是Google推出的容器集群，个人感觉对比docker原生的swarm，K8s更全面当然也更复杂。具体的对Kubernetes的介绍可以网上找，这里不多说了，下面开始记录一下手工搭建一个简单集群的步骤。<br>首先你的主机linux内核需要3.1以上，推荐ubuntu 16.04</p>
<h1 id="一、etcd"><a href="#一、etcd" class="headerlink" title="一、etcd"></a>一、etcd</h1><p>这是一个类似zookeeper的服务发现注册组件，介绍说在算法上比zk简单点，K8s使用这个etcd做服务注册，在K8s 1.6版本开始要使用etcd3。<br>可以从以下链接下载编译好的二进制安装包 etcd-v3.2.9-linux-amd64.tar.gz<br>解压后直接把里面的etcd、etcdctl文件cp到/usr/bin<br>编辑systemd配置 /usr/lib/systemd/system/etcd.service：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=etcd - highly-available key value store</span><br><span class="line">Documentation=https://github.com/coreos/etcd</span><br><span class="line">Documentation=man:etcd</span><br><span class="line">After=network.target</span><br><span class="line">Wants=network-online.target</span><br><span class="line">[Service]</span><br><span class="line">Environment=DAEMON_ARGS=</span><br><span class="line">Environment=ETCD_NAME=%H</span><br><span class="line">Environment=ETCD_DATA_DIR=/data/etcd</span><br><span class="line">EnvironmentFile=-/etc/default/%p</span><br><span class="line">Type=notify</span><br><span class="line">User=etcd</span><br><span class="line">PermissionsStartOnly=true</span><br><span class="line">#ExecStart=/bin/sh -c &quot;GOMAXPROCS=$(nproc) /usr/bin/etcd $DAEMON_ARGS&quot;</span><br><span class="line">ExecStart=/usr/bin/etcd $DAEMON_ARGS</span><br><span class="line">Restart=on-abnormal</span><br><span class="line">#RestartSec=10s</span><br><span class="line">LimitNOFILE=65536</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure>

<p>etcd配置文件放在 /etc/default/etcd， 现在是单机版，所以配置下面几个就可以：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ETCD_DATA_DIR=&quot;/data/etcd&quot;</span><br><span class="line">ETCD_LISTEN_PEER_URLS=&quot;http://0.0.0.0:2380&quot;</span><br><span class="line">ETCD_LISTEN_CLIENT_URLS=&quot;http://0.0.0.0:2379&quot;</span><br><span class="line">ETCD_ADVERTISE_CLIENT_URLS=&quot;http://0.0.0.0:2379&quot;</span><br></pre></td></tr></table></figure>

<p>配置完成后使用systemd启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl daemon-reload</span><br><span class="line">sudo systemctl enable etcd</span><br><span class="line">sudo systemctl start etcd</span><br></pre></td></tr></table></figure>

<p>验证一下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ubuntu@ubuntu1:/etc/default$ etcdctl cluster-health</span><br><span class="line">member 4550dff037975af3 is healthy: got healthy result from http://0.0.0.0:2379</span><br><span class="line">cluster is healthy</span><br></pre></td></tr></table></figure>

<h1 id="二、SSL证书"><a href="#二、SSL证书" class="headerlink" title="二、SSL证书"></a>二、SSL证书</h1><p>K8s各个组件与master之间的安全通信推荐使用基于CA签名的SSL证书加密数据，要求在master节点的kube-apiserver、kube-controller-manager、kube-scheduler进程及node节点的kubelet、kube-proxy进程配置CA签名证书。<br>使用easyrsa生成证书比较方便，先下载<a href="https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz" target="_blank" rel="noopener">easy-rsa.tar.gz</a><br>解压后进入 easy-rsa-master/easyrsa3 目录，编辑vars文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">set_var EASYRSA_REQ_COUNTRY    &quot;CN&quot;</span><br><span class="line">set_var EASYRSA_REQ_PROVINCE   &quot;Guangdong&quot;</span><br><span class="line">set_var EASYRSA_REQ_CITY       &quot;Zhongshan&quot;</span><br><span class="line">set_var EASYRSA_REQ_ORG        &quot;myself&quot;</span><br><span class="line">set_var EASYRSA_REQ_EMAIL      &quot;me@example.net&quot;</span><br><span class="line">set_var EASYRSA_REQ_OU         &quot;k8s-test&quot;</span><br></pre></td></tr></table></figure>

<p>初始化easyrsa环境：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./easyrsa init-pki</span><br></pre></td></tr></table></figure>

<p>生成ca证书：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./easyrsa --batch &quot;--req-cn=$&#123;MASTER_IP&#125;@`date +%s`&quot; build-ca nopass</span><br></pre></td></tr></table></figure>

<p>生成服务器证书和key：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">./easyrsa --subject-alt-name=&quot;IP:$&#123;MASTER_IP&#125;&quot;\</span><br><span class="line">&quot;IP:$&#123;MASTER_CLUSTER_IP&#125;,&quot;\</span><br><span class="line">&quot;DNS:kubernetes,&quot;\</span><br><span class="line">&quot;DNS:kubernetes.default,&quot;\</span><br><span class="line">&quot;DNS:kubernetes.default.svc,&quot;\</span><br><span class="line">&quot;DNS:kubernetes.default.svc.cluster,&quot;\</span><br><span class="line">&quot;DNS:kubernetes.default.svc.cluster.local&quot; \</span><br><span class="line">--days=10000 \</span><br><span class="line">build-server-full server nopass</span><br></pre></td></tr></table></figure>

<p>MASTER_CLUSTER_IP是apiserver服务启动参数–service-cluster-ip-range的第一个ip<br>然后复制pki/ca.crt, pki/issued/server.crt, pki/private/server.key到 master节点的/etc/kubernetes/pki目录备用</p>
<p>生成client证书：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./easyrsa build-client-full kubelet nopass</span><br></pre></td></tr></table></figure>

<p>复制pki/ca.crt, pki/issued/client.crt, pki/private/client.key到 node节点的/etc/kubernetes/pki目录备用</p>
<h1 id="三、master端"><a href="#三、master端" class="headerlink" title="三、master端"></a>三、master端</h1><p>下载k8s的二进制版，当前是v1.8.3 kubernetes-server-linux-amd64.tar.gz<br>把kube-apiserver、kube-controller-manager、kube-scheduler文件cp到/usr/bin</p>
<h2 id="3-1-kube-apiserver服务"><a href="#3-1-kube-apiserver服务" class="headerlink" title="3.1 kube-apiserver服务"></a>3.1 kube-apiserver服务</h2><p>编辑systemd配置 /usr/lib/systemd/system/kube-apiserver.service</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=Kubernetes API Server</span><br><span class="line">Documentation=https://github.com/kubernetes/kubernetes</span><br><span class="line">After=etcd.service</span><br><span class="line">Wants=etcd.service</span><br><span class="line">[Service]</span><br><span class="line">EnvironmentFile=/etc/kubernetes/apiserver</span><br><span class="line">ExecStart=/usr/bin/kube-apiserver $KUBE_API_ARGS</span><br><span class="line">Restart=on-failure</span><br><span class="line">Type=notify</span><br><span class="line">LimitNOFILE=65536</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure>

<p>配置文件/etc/kubernetes/apiserver</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">KUBE_API_ARGS=&quot;--storage-backend=etcd3 --etcd-servers=http://192.168.21.66:2379 --client-ca-file=/etc/kubernetes/pki/ca.crt --tls-cert-file=/etc/kubernetes/pki/server.crt --tls-private-key-file=/etc/kubernetes/pki/server.key --service-cluster-ip-range=169.169.0.0/16 --service-node-port-range=1-65535 --admission-control=NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,ResourceQuota,DefaultTolerationSeconds --logtostderr=false --log-dir=/var/log/kubernetes --v=2&quot;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>–etcd-servers： etcd的服务地址<br>–client-ca-file： ca证书文件<br>–tls-cert-file： 服务器证书文件<br>–tls-private-key-file： 服务器key文件<br>–service-cluster-ip-range： k8s集群中service服务的虚拟ip段，不能与物理ip冲突<br>–admission-control： 准入控制设置，各控制模块以插件形式依次生效</p>
</blockquote>
<p>创建好日志目录 /var/log/kubernetes</p>
<h2 id="3-2-kube-controller-manager服务"><a href="#3-2-kube-controller-manager服务" class="headerlink" title="3.2 kube-controller-manager服务"></a>3.2 kube-controller-manager服务</h2><p>kube-controller-manager依赖kube-apiserver<br>创建systemd配置 /usr/lib/systemd/system/kube-controller-manager.service ：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=Kubernetes Controller Manager</span><br><span class="line">Documentation=https://github.com/kubernetes/kubernetes</span><br><span class="line">After=kube-apiserver.service</span><br><span class="line">Requires=kube-apiserver.service</span><br><span class="line">[Service]</span><br><span class="line">EnvironmentFile=/etc/kubernetes/controller-manager</span><br><span class="line">ExecStart=/usr/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_ARGS</span><br><span class="line">Restart=on-failure</span><br><span class="line">LimitNOFILE=65536</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure>

<p>配置文件/etc/kubernetes/controller-manager</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">KUBE_CONTROLLER_MANAGER_ARGS=&quot;--master=https://192.168.21.66:6443 --kubeconfig=/etc/kubernetes/kubeconfig --service_account_private_key_file=/etc/kubernetes/pki/server.key --root-ca-file=/etc/kubernetes/pki/ca.crt --logtostderr=false --log-dir=/var/log/kubernetes --v=2&quot;</span><br></pre></td></tr></table></figure>

<p>1.8版本使用kubeconfig进行集群信息配置<br>/etc/kubernetes/kubeconfig</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: v1</span><br><span class="line">kind: Config</span><br><span class="line">users:</span><br><span class="line">- name: controllermanager</span><br><span class="line">  user:</span><br><span class="line">    client-certificate: /etc/kubernetes/pki/kube-cs-client.crt</span><br><span class="line">    client-key: /etc/kubernetes/pki/kube-cs-client.key</span><br><span class="line">clusters:</span><br><span class="line">- name: k8s-test</span><br><span class="line">  cluster:</span><br><span class="line">    certificate-authority: /etc/kubernetes/pki/ca.crt</span><br><span class="line">contexts:</span><br><span class="line">- context:</span><br><span class="line">    cluster: k8s-test</span><br><span class="line">    user: controllermanager</span><br><span class="line">  name: my-context</span><br><span class="line">current-context: my-context</span><br></pre></td></tr></table></figure>

<blockquote>
<p>–master： apiserver的服务地址，6443是加密https端口<br>–kubeconfig： 集中配置文件，包括证书等</p>
</blockquote>
<p>3.3 kube-scheduler服务<br>kube-scheduler服务依赖于kube-apiserver<br>创建systemd配置 /usr/lib/systemd/system/kube-scheduler.service：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=Kubernetes Scheduler</span><br><span class="line">Documentation=https://github.com/kubernetes/kubernetes</span><br><span class="line">After=kube-apiserver.service</span><br><span class="line">Requires=kube-apiserver.service</span><br><span class="line">[Service]</span><br><span class="line">EnvironmentFile=/etc/kubernetes/scheduler</span><br><span class="line">ExecStart=/usr/bin/kube-scheduler $KUBE_SCHEDULER_ARGS</span><br><span class="line">Restart=on-failure</span><br><span class="line">LimitNOFILE=65536</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure>

<p>配置文件/etc/kubernetes/scheduler:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">KUBE_SCHEDULER_ARGS=&quot;--master=https://192.168.21.66:6443 --kubeconfig=/etc/kubernetes/kubeconfig --logtostderr=false --log-dir=/var/log/kubernetes --v=2&quot;</span><br></pre></td></tr></table></figure>

<p>kubeconfig与kube-controller-manager一样</p>
<p>配置完成后使用systemd启动以上三个服务</p>
<h1 id="四、node端"><a href="#四、node端" class="headerlink" title="四、node端"></a>四、node端</h1><p>工作节点是实际运行容器的物理服务器，需要预先安装docker并启动，安装docker参考之前的博文<br>下载k8s的二进制版[kubernetes-server-linux-amd64.tar.gz]，复制kubelet、kube-proxy文件到/usr/bin</p>
<h2 id="4-1-kubele"><a href="#4-1-kubele" class="headerlink" title="4.1 kubele"></a>4.1 kubele</h2><p>创建systemd配置 /usr/lib/systemd/system/kubelet.service：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=Kubernetes Kubelet</span><br><span class="line">Documentation=https://github.com/kubernetes/kubernetes</span><br><span class="line">After=docker.service</span><br><span class="line">Requires=docker.service</span><br><span class="line">[Service]</span><br><span class="line">EnvironmentFile=/etc/kubernetes/kubelet</span><br><span class="line">WorkingDirectory=/var/lib/kubelet</span><br><span class="line">ExecStart=/usr/bin/kubelet $KUBELET_ARGS</span><br><span class="line">Restart=on-failure</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure>

<p>配置文件/etc/kubernetes/kubelet</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">KUBELET_ARGS=&quot;--kubeconfig=/etc/kubernetes/kubeconfig --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0 --fail-swap-on=false --logtostderr=false --log-dir=/var/log/kubernetes --v=2&quot;</span><br></pre></td></tr></table></figure>

<p>WorkingDirectory是保存数据的目录，预先创建</p>
<blockquote>
<p>–kubeconfig: 集中配置文件，旧版的–api-server参数废弃<br>–pod-infra-container-image： k8s pod使用的pause镜像地址，指定为阿里的<br>–fail-swap-on： 默认为true，即服务器不能有swap，这里测试环境忽略swap</p>
</blockquote>
<p>kubeconfig文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">apiVersion: v1</span><br><span class="line">clusters:</span><br><span class="line">- cluster:</span><br><span class="line">    server: https://192.168.21.66:6443</span><br><span class="line">    certificate-authority: /etc/kubernetes/pki/ca.crt</span><br><span class="line">  name: k8s-test</span><br><span class="line">contexts:</span><br><span class="line">- context:</span><br><span class="line">    cluster: k8s-test</span><br><span class="line">    user: kubelet</span><br><span class="line">  name: my-context</span><br><span class="line">current-context: my-context</span><br><span class="line">kind: Config</span><br><span class="line">preferences: &#123;&#125;</span><br><span class="line">users:</span><br><span class="line">- name: kubelet</span><br><span class="line">  user:</span><br><span class="line">    client-certificate: /etc/kubernetes/pki/kubelets.crt</span><br><span class="line">    client-key: /etc/kubernetes/pki/kubelets.key</span><br></pre></td></tr></table></figure>

<h2 id="4-2-kube-proxy"><a href="#4-2-kube-proxy" class="headerlink" title="4.2 kube-proxy"></a>4.2 kube-proxy</h2><p>创建systemd配置 /usr/lib/systemd/system/kube-proxy.service</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[Unit]</span><br><span class="line">Description=Kubernetes  Kube-proxy Server</span><br><span class="line">Documentation=https://github.com/kubernetes/kubernetes</span><br><span class="line">After=network.target</span><br><span class="line">Requires=networking.service</span><br><span class="line">[Service]</span><br><span class="line">EnvironmentFile=/etc/kubernetes/proxy</span><br><span class="line">ExecStart=/usr/bin/kube-proxy $KUBE_PROXY_ARGS</span><br><span class="line">Restart=on-failure</span><br><span class="line">LimitNOFILE=65536</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure>

<p>配置文件/etc/kubernetes/proxy</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">KUBE_PROXY_ARGS=&quot;--kubeconfig=/etc/kubernetes/kubeconfig --logtostderr=false --log-dir=/var/log/kubernetes --v=2&quot;</span><br></pre></td></tr></table></figure>

<p>kubeconfig和kubelet服务一致</p>
<p>配置成功后使用systemd启动kubelet和kube-proxy</p>
<h1 id="五、测试"><a href="#五、测试" class="headerlink" title="五、测试"></a>五、测试</h1><p>在master节点上运行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ubuntu@ubuntu1:/usr/local$ sudo kubectl get nodes</span><br><span class="line">NAME      STATUS    ROLES     AGE       VERSION</span><br><span class="line">wx        Ready     &lt;none&gt;    13d       v1.8.3</span><br></pre></td></tr></table></figure>

<p>可以看到node节点已经ready，可以增加RC、pod、service等资源了，当然现在所有资源都只能放在一个node上。</p>
<h1 id="六、后续"><a href="#六、后续" class="headerlink" title="六、后续"></a>六、后续</h1><p>这里只是演示搭建了master和node的关联配置，后续还有内部DNS服务、网络等需要完善</p>
  
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/K8s/">K8s</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/容器架构/">容器架构</a>
</div>



<div class="article-share" id="share">

  <div data-url="http://www.lstop.pub/2017/11/29/Kubernetes-1-8-基础集群搭建/" data-title="Kubernetes 1.8 基础集群搭建 | Zong&#39;s blog" data-tsina="" class="share clearfix">
  </div>

</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/12/04/Kubernetes-1-8-DNS服务/" title="Kubernetes 1.8 DNS服务">
  <strong>PREVIOUS:</strong><br/>
  <span>
  Kubernetes 1.8 DNS服务</span>
</a>
</div>


<div class="next">
<a href="/2017/10/23/docker-compose编排容器/"  title="docker-compose编排容器">
 <strong>NEXT:</strong><br/> 
 <span>docker-compose编排容器
</span>
</a>
</div>

</nav>

	
</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
  <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、etcd"><span class="toc-number">1.</span> <span class="toc-text">一、etcd</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、SSL证书"><span class="toc-number">2.</span> <span class="toc-text">二、SSL证书</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、master端"><span class="toc-number">3.</span> <span class="toc-text">三、master端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-kube-apiserver服务"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 kube-apiserver服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-kube-controller-manager服务"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 kube-controller-manager服务</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、node端"><span class="toc-number">4.</span> <span class="toc-text">四、node端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-kubele"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 kubele</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-kube-proxy"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 kube-proxy</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#五、测试"><span class="toc-number">5.</span> <span class="toc-text">五、测试</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#六、后续"><span class="toc-number">6.</span> <span class="toc-text">六、后续</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Airtest/" title="Airtest">Airtest<sup>1</sup></a></li>
		
			<li><a href="/tags/DNS/" title="DNS">DNS<sup>1</sup></a></li>
		
			<li><a href="/tags/GitLab/" title="GitLab">GitLab<sup>1</sup></a></li>
		
			<li><a href="/tags/K8s/" title="K8s">K8s<sup>8</sup></a></li>
		
			<li><a href="/tags/Linux/" title="Linux">Linux<sup>1</sup></a></li>
		
			<li><a href="/tags/MongoDB/" title="MongoDB">MongoDB<sup>2</sup></a></li>
		
			<li><a href="/tags/OpenWrt/" title="OpenWrt">OpenWrt<sup>1</sup></a></li>
		
			<li><a href="/tags/Python/" title="Python">Python<sup>2</sup></a></li>
		
			<li><a href="/tags/RabbitMQ/" title="RabbitMQ">RabbitMQ<sup>1</sup></a></li>
		
			<li><a href="/tags/calico/" title="calico">calico<sup>1</sup></a></li>
		
			<li><a href="/tags/cdn/" title="cdn">cdn<sup>1</sup></a></li>
		
			<li><a href="/tags/docker/" title="docker">docker<sup>3</sup></a></li>
		
			<li><a href="/tags/docker-registry/" title="docker registry">docker registry<sup>1</sup></a></li>
		
			<li><a href="/tags/elasticsearch/" title="elasticsearch">elasticsearch<sup>3</sup></a></li>
		
			<li><a href="/tags/elk/" title="elk">elk<sup>3</sup></a></li>
		
			<li><a href="/tags/k8s/" title="k8s">k8s<sup>3</sup></a></li>
		
			<li><a href="/tags/kubernetes/" title="kubernetes">kubernetes<sup>1</sup></a></li>
		
			<li><a href="/tags/nginx/" title="nginx">nginx<sup>1</sup></a></li>
		
			<li><a href="/tags/python/" title="python">python<sup>1</sup></a></li>
		
			<li><a href="/tags/tomcat/" title="tomcat">tomcat<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
      <li><a href="http://www.v2ex.com/?r=zong400" target="_blank" title="V2EX">V2EX</a></li>
      <li><a href="http://hexo.io" target="_blank" title="Hexo">Hexo</a></li>
	  <li><a href="https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=s0bh6uzq" target="_blank" title="阿里云">阿里云</a></li>
	  <li><a href="https://cloud.tencent.com/redirect.php?redirect=1014&cps_key=5bd9deb84d4d9f34b65fb934e12d03e3&from=console" target="_blank" title="腾讯云">腾讯云</a></li>
    </ul>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	
	<div class="social-font" class="clearfix">
		
		
		
		
	</div>
		<p class="copyright">Hosted by <a href="https://pages.coding.me/" target="_blank" title="Coding Pages">Coding Pages</a></p>
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wizicer/iceman" target="_blank" title="Iceman">Iceman</a> © 2020 
		
		<a href="http://www.lstop.pub" target="_blank" title="Zong">Zong</a>
		
		</p>
</div>
</footer>
    <script src="//cdn.staticfile.org/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#share"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="#textlogo" class="article-back-to-top" title="Top"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="QRcode"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="Weibo"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>









  </body>
</html>

